package com.beone.smart.smartBeauty.controller.appApi;

import com.beone.smart.smartBeauty.dao.ShopCarMapper;
import com.beone.smart.smartBeauty.exception.GlobalException;
import com.beone.smart.smartBeauty.model.dto.ShopCarGoodsDto;
import com.beone.smart.smartBeauty.model.entity.GoodsDO;
import com.beone.smart.smartBeauty.model.entity.ShopCarDo;
import com.beone.smart.smartBeauty.model.form.ShopCarCreateForm;
import com.beone.smart.smartBeauty.model.form.ShopCarUpdateForm;
import com.beone.smart.smartBeauty.model.result.ResultEntity;
import com.beone.smart.smartBeauty.model.result.ResultTool;
import com.beone.smart.smartBeauty.service.intf.GoodsService;
import com.beone.smart.smartBeauty.service.intf.ShopCarService;
import com.beone.smart.smartBeauty.utils.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * Created by bruce on 20/2/16 -- 16:57
 */
@Api(description = "购物车")
@Slf4j
@RestController
@RequestMapping(value = "/shopCar")
public class ShopCarController {
    @Autowired
    private ShopCarService shopCarService;
    @Autowired
    private ShopCarMapper shopCarMapper;
    @Autowired
    private GoodsService goodsService;

    @ApiOperation(value = "获取购物车列表")
    @GetMapping(value = "/list")
    public ResultEntity list() {
        String uid = RequestUtil.getUid();
        List<ShopCarGoodsDto> list = shopCarMapper.getList(uid);
        return ResultTool.ok(list);
    }

    @ApiOperation(value = "添加商品到购物车")
    @PostMapping(value = "/create")
    public ResultEntity create(@RequestBody ShopCarCreateForm shopCarCreateForm) {
        GoodsDO goods = goodsService.getById(shopCarCreateForm.getGoodsId());
        if (goods == null) {
            throw new GlobalException("商品id错误，查询为null");
        }
        ShopCarDo shopCarDo = new ShopCarDo();
        BeanUtils.copyProperties(shopCarCreateForm, shopCarDo);
        shopCarDo.setUserId(RequestUtil.getUid());
        shopCarDo.setPrice(goods.getPrice());
        return ResultTool.ok(shopCarService.save(shopCarDo));
    }

    @ApiOperation(value = "更新购物车中某一个商品的数量")
    @PostMapping(value = "/update")
    public ResultEntity update(@RequestBody ShopCarUpdateForm shopCarForm) {
        ShopCarDo shopCarDo = new ShopCarDo();
        BeanUtils.copyProperties(shopCarForm, shopCarDo);
        return ResultTool.ok(shopCarService.updateById(shopCarDo));
    }

    @ApiOperation(value = "从购物车中移除某一个商品出去", notes = "此处id是购物车列表中的那个id，不是具体的商品id")
    @ApiImplicitParam(name = "id", value = "购物车id", required = true, dataType = "String")
    @GetMapping(value = "/delete")
    public ResultEntity delete(@RequestParam @NotBlank String id) {
        ShopCarDo shopCarDo = shopCarService.getById(id);
        if (shopCarDo == null) {
            throw new GlobalException("查询为null，请检查id");
        }
        return ResultTool.ok(shopCarService.removeById(id));
    }
}
